﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Raven.Client.Embedded;
using System.Reflection;
using System.Diagnostics;

namespace FangbuchWCFServiceApp
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        private EmbeddableDocumentStore documentStore;

        public Service1()
        {
            

            var pathToDataBase = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var documentStore = new EmbeddableDocumentStore { DataDirectory = pathToDataBase };
            
            documentStore.Initialize();
            Debug.Assert(documentStore != null);
        }

        public string GetData(int value)
        {
            var myCompany = new Company
            {
                Name = "Hibernating Rhinos",
                Employees =
                            {
                                new Employee
                                    {
                                        Name = "Ayende Rahien"
                                    }
                            },
                Country = "Israel"
            };

            var pathToDataBase = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var documentStore = new EmbeddableDocumentStore { DataDirectory = pathToDataBase };

            documentStore.Initialize();

            // Store the company in our RavenDB server
            using (var session = documentStore.OpenSession())
            {
                session.Store(myCompany);
                session.SaveChanges();
            }

            // Create a new session, retrieve an entity, and change it a bit
            using (var session = documentStore.OpenSession())
            {
                Company entity = session.Query<Company>()
                    .Where(x => x.Country == "Israel")
                    .FirstOrDefault();

                // We can also load by ID: session.Load<Company>(companyId);

                entity.Name = "Another Company";
                session.SaveChanges(); // will send the change to the database
            }

            return myCompany.ToString();
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }


        public string GetCompanyName()
        {
            var pathToDataBase = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var documentStore = new EmbeddableDocumentStore { DataDirectory = pathToDataBase };
            var result = string.Empty;
            documentStore.Initialize();

            // Create a new session, retrieve an entity, and change it a bit
            using (var session = documentStore.OpenSession())
            {
                Company entity = session.Query<Company>().FirstOrDefault();

                // We can also load by ID: session.Load<Company>(companyId);

                result = entity.Name = "Another Company";
                
            }

            return result;
        }
    }
}
